package cn.biplam.back.operator.ddl.sql2md;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.TableColBufCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 字段缓冲区数据操作类 
 * <li>  插入源数据表字段到缓存区
 * <li>  更新源数据表字段信息
 * <li>  数据操作类的所有访问必须有模型类型参数
 * @author XiaoRuxing 2009-6-26下午09:01:51
 * @version 1.0
 */
public class TableColBuffer extends DataOperator{

	public TableColBuffer() throws ErrorMessage{
		super("src_table_col_buffer");
	}

	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(opId.equals(TableColBufCode.Insert)){
			this.insert(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(TableColBufCode.Update)){
			this.update(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(TableColBufCode.Select_By_Column_Name)){
			this.getByName(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(TableColBufCode.Select_By_TableID_And_Batch)){
			this.getByWoidAndBatch(sql);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(TableColBufCode.Select_By_ID)){
			this.getByID(sql,filter);
			return this.doing(sql, Operator_Type.Select);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}

	/**
	 * 更新表字段信息
	 * @throws ErrorMessage 
	 */
	public void update(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		Update update=new Update(this.tableName);
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_ORDER, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_ORDER));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_NAME, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_NAME));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_CN_NAME, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_CN_NAME));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_COMMENTS, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_COMMENTS));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_TYPE, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_TYPE));
		update.putFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_LENGTH, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_LENGTH));
		update.putFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_PRECISION, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_PRECISION));
		update.putFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_SCALE, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_SCALE));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.IS_PK, filter.getValue(MdStruc.SrcTabColBufStruc.IS_PK));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.NULL_ABLE, filter.getValue(MdStruc.SrcTabColBufStruc.NULL_ABLE));
		update.setWhere(MdStruc.SrcTabColBufStruc.COLUMN_ID+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_ID)+"'");
		sql.add(update.getSimpleSql());
	}

	/**
	 * 插入表字段到缓存区  
	 */
	public void insert(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		String ocid=generatorRowID();
		String woid=filter.getValue(MdStruc.SrcTabColBufStruc.TABLE_ID);
		String orid=filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_ORDER);
		Insert insert=new Insert(this.tableName);
		insert.putStringFieldDenyNull(MdStruc.SrcTabColBufStruc.COLUMN_ID, ocid);
		insert.putStringFieldDenyNull(MdStruc.SrcTabColBufStruc.COLUMN_ORDER, orid);
		insert.putStringFieldDenyNull(MdStruc.SrcTabColBufStruc.TABLE_ID, woid);
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_NAME, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_CN_NAME, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_CN_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.COLUMN_COMMENTS, filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_COMMENTS));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_TYPE, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_TYPE));
		insert.putFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_LENGTH, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_LENGTH));
		insert.putFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_PRECISION, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_PRECISION));
		insert.putFieldIgnoreNull(MdStruc.SrcTabColBufStruc.DATA_SCALE, filter.getValue(MdStruc.SrcTabColBufStruc.DATA_SCALE));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.IS_PK, filter.getValue(MdStruc.SrcTabColBufStruc.IS_PK));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.IF_FK, filter.getValue(MdStruc.SrcTabColBufStruc.IF_FK));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.NULL_ABLE, filter.getValue(MdStruc.SrcTabColBufStruc.NULL_ABLE));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabColBufStruc.BATCH, filter.getValue(MdStruc.SrcTabColBufStruc.BATCH));
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 * 更新外键标志,设置指定批次指定表指定字段外键标志位1<br>
	 * 需要参数： 表ID,批次号,外键字段名
	 */
	public void updateFkFlag(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		Update update = new Update(this.tableName);
		update.putStringFieldDenyNull(MdStruc.SrcTabColBufStruc.IF_FK, "1");
		StringBuilder sp = new StringBuilder();
		sp.append(MdStruc.SrcTabColBufStruc.TABLE_ID+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.TABLE_ID)+"'");
		sp.append(" and ");
		sp.append(MdStruc.SrcTabColBufStruc.COLUMN_NAME+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_NAME)+"'");
		sp.append(" and ");
		sp.append(MdStruc.SrcTabColBufStruc.BATCH+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.BATCH)+"'");
		update.setWhere(sp.toString());
		sql.add(update.getSimpleSql());
	}

	/** 根据字段名、表ID和批次号查询字段信息<br>
	 * 参数：
	 * <li>字段名
	 * <li>表ID
	 * <li>批次号
	 * @throws ErrorMessage 
	 */
	public void getByName(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp = new StringBuilder();
		sp.append("select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(MdStruc.SrcTabColBufStruc.TABLE_ID+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.TABLE_ID)+"'");
		sp.append(" and ");
		sp.append(MdStruc.SrcTabColBufStruc.COLUMN_NAME+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_NAME)+"'");
		sp.append(" and ");
		sp.append(MdStruc.SrcTabColBufStruc.BATCH+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.BATCH)+"'");
		sql.add(sp.toString());
	}
	
	/** 根据列ID查询字段信息<br>
	 * 参数：
	 * <li>列ID
	 * @throws ErrorMessage 
	 */
	public void getByID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp = new StringBuilder();
		sp.append("select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(MdStruc.SrcTabColBufStruc.COLUMN_ID+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.COLUMN_ID)+"'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据表ID和任务ID取列信息
	 * @param sql
	 * @throws ErrorMessage 
	 */
	public void getByWoidAndBatch(ArrayList<String> sql) throws ErrorMessage{
		_init(filter);
		StringBuilder sp = new StringBuilder();
		sp.append("select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(MdStruc.SrcTabColBufStruc.TABLE_ID+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.TABLE_ID)+"'");
		sp.append(" and ");
		sp.append(MdStruc.SrcTabColBufStruc.BATCH+"='"+filter.getValue(MdStruc.SrcTabColBufStruc.BATCH)+"'");
		sp.append(" order by ");
		sp.append(MdStruc.SrcTabColBufStruc.COLUMN_ORDER);
		sql.add(sp.toString());
	}
	
	/** 
	 * 删除缓存区表信息<br>
	 */
	public void deleteByTaskID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		String taskId=null;
		taskId=filter.getValue(MdStruc.SrcTabColBufStruc.BATCH);
		Delete delete=new Delete(this.tableName);
		delete.setWhere(MdStruc.SrcTabColBufStruc.BATCH+"='"+taskId+"'");
		sql.add(delete.getSimpleSql());
	}

	/**
	 * 根据模型类型初始化表名  
	 */
	private void _init(DataFilter filter) throws ErrorMessage{
		String modelType = filter.getValue(MdStruc.SrcTabColBufStruc.MODEL_TYPE);
		if(TOOLS.StringIsEmpty(modelType)){
			throw new ErrorMessage("Model type is null");
		}
		this.tableName = "src_table_col_buffer";
	}
}
